package com.miexample.ec.commodity.service.impl;

import com.miexample.ec.commodity.dao.CommodityDao;
import com.miexample.ec.commodity.service.CommodityInfoService;
import com.miexample.ec.common.pojo.Commodity;
import org.slf4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

/**
 * CommodityInfoServiceImpl
 *
 * @author reck
 * @date 2018/06/22
 */
public class CommodityInfoServiceImpl implements CommodityInfoService {
    private static final Logger logger = LoggerFactory.getLogger(CommodityInfoServiceImpl.class);

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Override
    public Commodity getCommodity(long id) {
        CommodityDao commodityDao = sqlSessionTemplate.getMapper(CommodityDao.class);
        Commodity commodity = commodityDao.get(id);
        return commodity;
    }

    @Override
    public boolean saveCommodity(Commodity commodity) {
        CommodityDao commodityDao = sqlSessionTemplate.getMapper(CommodityDao.class);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        commodity.setGmtCreate(simpleDateFormat.format(System.currentTimeMillis()));
        commodity.setGmtModified(simpleDateFormat.format(System.currentTimeMillis()));
        return commodityDao.save(commodity);
    }
}
